using System.Collections;
using System.Text;
using TMPro;
using UnityEngine;

public class UI_SkillToolTip : UI_ToolTip
{
    private UI uI;
    private UI_SkillTree skillTree;
    [Header("Skill Details")]
    [SerializeField] private TextMeshProUGUI skillName;
    [SerializeField] private TextMeshProUGUI skillDescription;
    [SerializeField] private TextMeshProUGUI skillRequirements;

    [Header("Skill Details Display")]
    [SerializeField] private string metConditionHex;
    [SerializeField] private string notMetConditionHex;
    [SerializeField] private string importantInfoHex;
    [SerializeField] private Color color;

    [SerializeField] private string lockedSkillText = "You've taken a diffierent path - this skill is now locked.";

    private Coroutine blinkEffectCoroutine;
    protected override void Awake()
    {
        base.Awake();
        uI = GetComponentInParent<UI>();
        skillTree = uI.GetComponentInChildren<UI_SkillTree>(true);
    }
    public void ShowToolTip(bool isShow, RectTransform targetRect, UI_TreeNode node)
    {
        base.ShowToolTip(isShow, targetRect);
        if (isShow == false)
            return;
        skillName.text = node.skillData.displayName;
        skillDescription.text = node.skillData.descrpition;
        string skillLockedText = GetColorText(importantInfoHex, lockedSkillText);
        
        //判断当前鼠标悬停的技能是否锁定，如果锁定，则输出LockedSkillText，如果没有锁定，则构建信息
        skillRequirements.text = node.isLocked ? skillLockedText : GetRequirements(node.skillData.cost, node.neededNodes, node.conflictNodes);
    }

    //构造技能简介面板的信息
    private string GetRequirements(int skillCost, UI_TreeNode[] neededNodes, UI_TreeNode[] conflictNodes)
    {
        StringBuilder stringBuilder = new();
        stringBuilder.AppendLine("Requirements");
        string costColor = skillTree.EnoughSkillPoints(skillCost) ? metConditionHex : notMetConditionHex;
        stringBuilder.AppendLine($"<color={costColor}>- {skillCost} skill point(s) </color>");
        foreach (var node in neededNodes)
        {
            if (node == null) continue;
            string nodeColor = node.isUnlocked ? metConditionHex : notMetConditionHex;
            stringBuilder.AppendLine($"<color={nodeColor}>- {node.skillData.displayName} skill point(s) </color>");
        }
        if (conflictNodes.Length <= 0)
            return stringBuilder.ToString();
        stringBuilder.AppendLine(); //space
        stringBuilder.AppendLine($"<color={importantInfoHex}>Locks out: </color>");
        foreach (var node in conflictNodes)
        {
            if (node == null) continue;
            stringBuilder.AppendLine($"<color={importantInfoHex}>- {node.skillData.displayName}</color>");
        }
        return stringBuilder.ToString();
    }

    //开启技能简介面板锁定警报协程
    public void StartTextBlinkEffectCo()
    {
        StopLockedSkillEffect();
        blinkEffectCoroutine = StartCoroutine(TextBlinkEffectCo(.1f, 3));
    }
    public void StopLockedSkillEffect()
    {
        if (blinkEffectCoroutine != null)
            StopCoroutine(blinkEffectCoroutine);
    }
    //技能简介面板锁定警报协程
    private IEnumerator TextBlinkEffectCo(float blinkDuration, int blinkCount)
    {
        for (int i = 0; i < blinkCount; i++)
        {
            skillRequirements.text = GetColorText(notMetConditionHex, lockedSkillText);
            yield return new WaitForSeconds(blinkDuration);
            skillRequirements.text = GetColorText(importantInfoHex, lockedSkillText);
            yield return new WaitForSeconds(blinkDuration);
        }
    }
    //得到带颜色属性的文本
    private string GetColorText(string color, string text) => $"<color={color}>{text}</color>";
}
